﻿using CommandProcessor.Command;
using Data.Infrastructure;
using Data.Models;
using Data.Repositories;
using Domain.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Handlers
{
    public class CreateOrUpdateCompanyHandler : ICommandHandler<CreateOrUpdateCompanyCommand>
    {
        private readonly ICompanyRepository companyRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateCompanyHandler(ICompanyRepository companyRepository, IUnitOfWork unitOfWork)
        {
            this.companyRepository = companyRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateCompanyCommand command)
        {
            var company = new Company
            {
                ID = command.ID,
                Business=command.Business,
                Description = command.Description,
                DisplayName=command.DisplayName,
                Email=command.Email,
                Logo=command.Logo,
                OrgaType=command.OrgaType,
                ParentID=command.ParentID,
                PostAddress=command.PostAddress,
                PostAttention=command.PostAttention,
                Tel_Num=command.Tel_Num,
                TradingName=command.TradingName,
                Website=command.Website,
                
            };
            if (company.ID == 0)
                companyRepository.Add(company);
            else
                companyRepository.Update(company);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateCompanyCommand> commands)
        {
            throw new NotImplementedException();
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateCompanyCommand> commands)
        {
            throw new NotImplementedException();
        }
    }
}
